package com.karry.yl.web.services;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.karry.commons.base.SuperController;
import com.karry.commons.vo.JsonResponse;
import com.karry.commons.vo.LayTableResponse;
import com.karry.yl.domain.baseinfo.Room;
import com.karry.yl.domain.elder.CheckInRecords;
import com.karry.yl.domain.services.RoundsDdeviant;
import com.karry.yl.domain.services.RoundsNormal;
import com.karry.yl.domain.services.RoundsRecord;
import com.karry.yl.service.baseinfo.RoomService;
import com.karry.yl.service.elder.CheckInRecordsService;
import com.karry.yl.service.services.RoundsDeviantService;
import com.karry.yl.service.services.RoundsNormalService;
import com.karry.yl.service.services.RoundsRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/roundsRecord")
public class RoundsRecordController extends SuperController {

    @Autowired
    private RoundsRecordService roundsRecordService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private RoundsNormalService roundsNormalService;
    @Autowired
    private RoundsDeviantService roundsDeviantService;
    @Autowired
    private CheckInRecordsService checkInRecordsService;
    @RequestMapping
    public String listUI(){
        return "services/roundsrecord/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public LayTableResponse listUI(int page, int limit){
        Page<RoundsRecord> itemsPage = roundsRecordService.selectPage(new Page<RoundsRecord>(page, limit),new EntityWrapper<RoundsRecord>().eq("orgId",currentUser.getOrgId()).eq("deleted",false).orderBy("createdTime",false));
        return new LayTableResponse(itemsPage.getRecords(),itemsPage.getTotal());
    }

    @RequestMapping("rounds")
    public String rounds(Long id,ModelMap map) {
        Room room=new Room();
        room.setOrgId(currentUser.getOrgId());
        List<Room> roomList = roomService.selectRoomsList(room);
        //房间楼宇格式转换
        Map<String,Map<Integer,List<Room>>> buildingMap = new LinkedHashMap<>();
        for(Room r : roomList){
            String buildingName = r.getBuildingName();
            if(buildingMap.containsKey(buildingName)){
                Map<Integer, List<Room>> floorMap = buildingMap.get(buildingName);  //楼层的Map
                int floor = r.getFloor();  //房间所在楼层
                if(floorMap.containsKey(floor)){
                    List<Room> rooms = floorMap.get(floor);
                    rooms.add(r);
                }else {
                    ArrayList<Room> rooms = new ArrayList<>();
                    rooms.add(r);
                    floorMap.put(floor,rooms);
                }
            }else{
                Map<Integer, List<Room>> floorMap = new LinkedHashMap<>();  //楼层的Map
                buildingMap.put(buildingName,floorMap);
                int floor = r.getFloor();  //房间所在楼层
                List<Room> rooms = new ArrayList<>();
                rooms.add(r);
                floorMap.put(floor,rooms);
            }
        }
        map.put("buildingMap", buildingMap);
        map.put("recordId", id);
        RoundsNormal roundsNormal = new RoundsNormal();
        roundsNormal.setOrgId(currentUser.getOrgId());
        roundsNormal.setRecordId(id);
        List<String> yichaRooms = roundsNormalService.selectYichaRoom(roundsNormal);
        map.put("yichaRooms", yichaRooms);
        CheckInRecords CheckInRecords=new CheckInRecords();
        CheckInRecords.setOrgId(currentUser.getOrgId());
        List<String> yizhuElderRooms= checkInRecordsService.selectYizhuElderRooms(CheckInRecords);
        map.put("yizhuElderRooms", yizhuElderRooms);
        return "services/roundsrecord/rounds";
    }
    @RequestMapping("/saveList")
    @ResponseBody
    public JsonResponse saveList(Long[] elderlyId, String[] elderNo,String[] elderlyName,String[] roundsStatus, String[] deviantRemark, String[] deviantResult,Long recordId,String roomNo,String buildingNo,String floor,Long  buildingId) {
        for(int i = 0;i<roundsStatus.length;i++){
            RoundsNormal roundsNormal = new RoundsNormal();
            roundsNormal.setOrgId(currentUser.getOrgId());
            roundsNormal.setElderlyId(elderlyId[i]);
            roundsNormal.setElderlyName(elderlyName[i]);
            roundsNormal.setRecordId(recordId);
            roundsNormal.setRoomCode(roomNo);
            roundsNormal.setRoundsStatus(roundsStatus[i]);
            roundsNormal.setBuildingNo(buildingNo);
            roundsNormal.setFloor(floor);
            roundsNormal.setStatus("1");
            roundsNormal.setCfUser(currentUser.getUserName());
            roundsNormal.setElderNo(elderNo[i]);
            roundsNormal.setBuildingId(buildingId);
            RoundsDdeviant roundsDdeviant=new RoundsDdeviant();
            roundsDdeviant.setOrgId(currentUser.getOrgId());
            roundsDdeviant.setElderlyId(elderlyId[i]);
            roundsDdeviant.setElderlyName(elderlyName[i]);
            roundsDdeviant.setRecordId(recordId);
            roundsDdeviant.setRoomCode(roomNo);
            roundsDdeviant.setRoundsStatus(roundsStatus[i]);
            roundsDdeviant.setBuildingNo(buildingNo);
            roundsDdeviant.setFloor(floor);
            roundsDdeviant.setStatus("0");
            roundsDdeviant.setCfUser(currentUser.getUserName());
            roundsDdeviant.setElderNo(elderNo[i]);
            roundsDdeviant.setBuildingId(buildingId);
            roundsNormalService.insert(roundsNormal);
            if(roundsStatus[i].equals("1")){
                    roundsDdeviant.setDeviantRemark(deviantRemark.length > 0 ? deviantRemark[i] : "");
                    roundsDdeviant.setDeviantResult(deviantResult.length > 0 ? deviantResult[i] : "");
                    roundsDeviantService.insert(roundsDdeviant);
            }
        }
        RoundsRecord roundsRecord=new RoundsRecord();
        roundsRecord.setId(recordId);
        roundsRecord.setStatus("1");
        roundsRecordService.updateById(roundsRecord);
        return JsonResponse.success("rooms","");
    }
    @RequestMapping("/update")
    @ResponseBody
    public JsonResponse update(RoundsRecord roundsRecord){
        roundsRecord.setStatus("2");
        roundsRecordService.updateById(roundsRecord);
        return JsonResponse.success("","");
    }
    @RequestMapping("roundsDeail")
    public String roundsDeail(Long id,ModelMap map) {
        RoundsNormal roundsNormal =new RoundsNormal();
        roundsNormal.setOrgId(currentUser.getOrgId());
        roundsNormal.setRecordId(id);
        List<RoundsDdeviant> roundsDdeviants = roundsNormalService.selectRoundsList(roundsNormal);

        //格式转换便于前台 合并单元格展示
        Map<String,Map<String,Map<String,List<RoundsDdeviant>>>> buildingMap = new LinkedHashMap<>();
        for(RoundsDdeviant r : roundsDdeviants){
            String buildingNo = r.getBuildingNo();
            if(buildingMap.containsKey(buildingNo)){
                Map<String, Map<String, List<RoundsDdeviant>>> floorMap = buildingMap.get(buildingNo);
                String floor = r.getFloor();
                if(floorMap.containsKey(floor)){
                    Map<String, List<RoundsDdeviant>> roomMap = floorMap.get(floor);
                    String roomNo = r.getRoomCode();
                    if(roomMap.containsKey(roomNo)){
                        List<RoundsDdeviant> rdList = roomMap.get(roomNo);
                        rdList.add(r);
                    }else {
                        List<RoundsDdeviant> rdList = new ArrayList<>();
                        rdList.add(r);
                        roomMap.put(roomNo,rdList);
                    }
                }else{
                    Map<String, List<RoundsDdeviant>> roomMap = new LinkedHashMap<>();
                    floorMap.put(floor,roomMap);
                    List<RoundsDdeviant> rdList = new ArrayList<>();
                    rdList.add(r);
                    roomMap.put(r.getRoomCode(),rdList);
                }
            }else{
                Map<String, Map<String, List<RoundsDdeviant>>> floorMap = new LinkedHashMap<>();
                buildingMap.put(buildingNo,floorMap);
                Map<String, List<RoundsDdeviant>> roomMap = new LinkedHashMap<>();
                floorMap.put(r.getFloor(),roomMap);
                List<RoundsDdeviant> rdList = new ArrayList<>();
                rdList.add(r);
                roomMap.put(r.getRoomCode(),rdList);
            }
        }

        map.put("buildingMap", buildingMap);
        return "services/roundsrecord/roundsDetails";
    }
}
